Completed
Pull Request — master (#227)
by Sander
03:18
created

angular.controller(ꞌRevisionCtrlꞌ)   C

Complexity

Conditions 8
Paths 64

Size

Total Lines 115

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 115
rs 5.2676
cc 8
nc 64
nop 12

How to fix   Long Method    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
/**
2
 * Nextcloud - passman
3
 *
4
 * @copyright Copyright (c) 2016, Sander Brand ([email protected])
5
 * @copyright Copyright (c) 2016, Marcos Zuriaga Miguel ([email protected])
6
 * @license GNU AGPL version 3 or any later version
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License as
10
 * published by the Free Software Foundation, either version 3 of the
11
 * License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 */
22
23
(function () {
24
	'use strict';
25
26
27
	/**
28
	 * @ngdoc function
29
	 * @name passmanApp.controller:RevisionCtrl
30
	 * @description
31
	 * # RevisionCtrl
32
	 * Controller of the passmanApp
33
	 */
34
	angular.module('passmanApp')
35
		.controller('RevisionCtrl', ['$scope', 'SettingsService', 'VaultService', 'CredentialService', '$location', '$routeParams', '$rootScope', 'NotificationService', '$filter', 'ShareService', 'EncryptService', '$translate',
36
			function ($scope, SettingsService, VaultService, CredentialService, $location, $routeParams, $rootScope, NotificationService, $filter, ShareService, EncryptService, $translate) {
37
				$scope.active_vault = VaultService.getActiveVault();
38
				if (!SettingsService.getSetting('defaultVault') || !SettingsService.getSetting('defaultVaultPass')) {
39
					if (!$scope.active_vault) {
40
						$location.path('/');
41
					}
42
				} else {
43
					if (SettingsService.getSetting('defaultVault') && SettingsService.getSetting('defaultVaultPass')) {
44
						var _vault = angular.copy(SettingsService.getSetting('defaultVault'));
45
						_vault.vaultKey = SettingsService.getSetting('defaultVaultPass');
46
						VaultService.setActiveVault(_vault);
47
						VaultService.getVault(_vault).then(function (vault) {
48
							vault.vaultKey = SettingsService.getSetting('defaultVaultPass');
49
							VaultService.setActiveVault(vault);
50
							$scope.active_vault = vault;
51
							$scope.$parent.selectedVault = true;
52
						});
53
					}
54
				}
55
56
				if ($scope.active_vault) {
57
					$scope.$parent.selectedVault = true;
58
				}
59
				var storedCredential = SettingsService.getSetting('revision_credential');
60
61
				var getRevisions = function () {
62
					CredentialService.getRevisions($scope.storedCredential.guid).then(function (revisions) {
63
						$scope.revisions = revisions;
64
					});
65
				};
66
67
				if (!storedCredential) {
68
					CredentialService.getCredential($routeParams.credential_id).then(function (result) {
69
						$scope.storedCredential = CredentialService.decryptCredential(angular.copy(result));
70
						getRevisions();
71
					});
72
				} else {
73
					$scope.storedCredential = CredentialService.decryptCredential(angular.copy(storedCredential));
74
					getRevisions();
75
				}
76
77
				$scope.selectRevision = function (revision) {
78
					var key;
79
					$scope.selectedRevision = angular.copy(revision);
80
81
					if (!$scope.storedCredential.hasOwnProperty('acl') && $scope.storedCredential.hasOwnProperty('shared_key')) {
82
						if ($scope.storedCredential.shared_key) {
83
							key = EncryptService.decryptString(angular.copy($scope.storedCredential.shared_key));
84
						}
85
					}
86
					if ($scope.storedCredential.hasOwnProperty('acl')) {
87
						key = EncryptService.decryptString(angular.copy($scope.storedCredential.acl.shared_key));
88
					}
89
90
					if (key) {
91
						$scope.selectedRevision.credential_data = ShareService.decryptSharedCredential(angular.copy(revision.credential_data), key);
92
					} else {
93
						$scope.selectedRevision.credential_data = CredentialService.decryptCredential(angular.copy(revision.credential_data));
94
					}
95
96
					$rootScope.$emit('app_menu', true);
97
				};
98
99
				$scope.closeSelected = function () {
100
					$rootScope.$emit('app_menu', false);
101
					$scope.selectedRevision = false;
102
				};
103
104
				$scope.deleteRevision = function (revision) {
105
					CredentialService.deleteRevision($scope.storedCredential.guid, revision.revision_id).then(function () {
106
						for (var i = 0; i < $scope.revisions.length; i++) {
107
							if ($scope.revisions[i].revision_id === revision.revision_id) {
108
								$scope.revisions.splice(i, 1);
109
								NotificationService.showNotification($translate.instant('revision.deleted'), 5000);
110
								break;
111
							}
112
						}
113
					});
114
				};
115
116
				$scope.restoreRevision = function (revision) {
117
					var key;
118
					var _revision = angular.copy(revision);
119
					var _credential = _revision.credential_data;
120
121
					if (!$scope.storedCredential.hasOwnProperty('acl') && $scope.storedCredential.hasOwnProperty('shared_key')) {
122
						if ($scope.storedCredential.shared_key) {
123
							key = EncryptService.decryptString(angular.copy($scope.storedCredential.shared_key));
124
						}
125
					}
126
					if ($scope.storedCredential.hasOwnProperty('acl')) {
127
						key = EncryptService.decryptString(angular.copy($scope.storedCredential.acl.shared_key));
128
					}
129
					if (key) {
130
						_credential = ShareService.encryptSharedCredential(_credential, key);
131
					}
132
					delete _credential.shared_key;
133
134
					//Used in activity
135
					_credential.revision_created = $filter('date')(_revision.created * 1000, "dd-MM-yyyy @ HH:mm:ss");
136
					CredentialService.updateCredential(_credential, (key)).then(function () {
137
						SettingsService.setSetting('revision_credential', null);
138
						$rootScope.$emit('app_menu', false);
139
						$location.path('/vault/' + $routeParams.vault_id);
140
						NotificationService.showNotification($translate.instant('revision.restored'), 5000);
141
					});
142
				};
143
144
				$scope.cancelRevision = function () {
145
					$location.path('/vault/' + $routeParams.vault_id);
146
					$scope.storedCredential = null;
147
					SettingsService.setSetting('revision_credential', null);
148
				};
149
150
			}]);
151
152
}());